<?php
    error_reporting(E_ALL); 
    date_default_timezone_set("Asia/Saigon");
    require_once("config.php");     
    require_once("functions.php");     
    require_once('libs/nusoap.php');

    $server = new soap_server();

    $ns = "urn:mTopSMSServiceReveiceMTMe";
    $server->configureWSDL("mTopServices", $ns);
    $server->wsdl->schemaTargetNamespace = $ns;    

    $server->wsdl->addComplexType(
    'response'
    , 'complexType'
    , 'struct'
    , 'all'
    , ''
    , array(
    'status'=>array(
    'name'=>'status'
    , 'type'=>'xsd:int'
    ), 
    'msg'=>array(
    'name'=>'msg'
    , 'type'=>'xsd:string'
    ), 
    'length'=>array(
    'name'=>'length'
    , 'type'=>'xsd:int'
    )
    )
    );           

    $server->register(
    "receiveMT"
    , array("MOId"=>"xsd:string", "Telco"=>"xsd:string", "ServiceNum"=>"xsd:string", "Phone"=>"xsd:string",
     "MsgType"=>"xsd:string", "Title"=>"xsd:string", "Content"=>"xsd:string", "Charging"=>"xsd:string" ,
     "RequestId"=>"xsd:string", "Token"=>"xsd:string")
   // , array("return"=> "tns:response")
    , array("return"=> "xsd:string")
    , $ns
    , $ns . "#receiveMT"
    , "rpc"            
    , "encoded"
    , "Examination Results"  
    );    

    function receiveMT($MOId, $Telco, $ServiceNum, $Phone,$MsgType,$Title,$Content,$Charging,$RequestId,$Token)
    {  
      // Check token
      //MD5(MOId+Telco+ServiceNum+Phone+MsgType+Title+Content+Charging+RequestId+PrivateKey)
      $privateKey= "wIOsFEeLoNc6mOf";
      $response = "0";
      $tokenCheck =  MD5($MOId.$Telco.$ServiceNum.$Phone.$MsgType.$Title.$Content.$Charging.$RequestId.$privateKey);
      
      $datalog = "MOId=".$MOId."|mt=".$Content."|"."serviceNumber=".$ServiceNum."|"."mobile=".$Phone."|"."requestId=".$RequestId."|status".$Charging."|Token=".$Token."|Telco=".$Telco."|MsgType=".$MsgType."|Title=".$Title;
      
     /* if(strcasecmp($Token,$tokenCheck)!=0) {
            $response = "-1"; // Sai token
            $datalog .="|response=".$response;
            writeLog($datalog,"/logSmsMTMe.txt");  
            return $response;
      }*/
      
       // Lay ban tin sms log voi MOId
       $rowSMSLog = getSMSLogByTransactionId($MOId);
       if(empty($rowSMSLog))
       {
           $response = "-3"; // MOid khong ton tai
       }else{
           $responseId = $rowSMSLog["responseId"];
          if(strcasecmp($responseId,"0")!=0){
              // Da xu ly
              $response = "-2"; 
          }else{
            // Câp nhật lại trạng thái tính tiền
             $response = "0";
            
          }
       }
       //Send MT 
       $resultSendMT = 0;
       if($response==0){
           $arrMo = explode(" ", $rowSMSLog["mo"]);
           $cmdCode= $arrMo[0];
           $operator = checkTelcoGateWay($Phone);
           
           $resultSendMT = sendMToGateWay( $ServiceNum,$Phone,$cmdCode,$Content,$MOId,$operator, $Charging,  0);
           // Neu $resultSendMT != 0 thi phai tinh toan de gui lai MT bang 1 proces khac
           $logMTToGateWay = "|ServiceNum=".$ServiceNum."|Phone=".$Phone."|cmdCode=".$mdCode."|Content=".$Content."|MOId=".$MOId."|operator=".$operator."|Charging=".$Charging."|resultSendMT=".$resultSendMT;
           writeLog($logMTToGateWay,"/logSmsSendMTMeToGateway.txt");  
           
           $resultSendMT = $resultSendMT == -1?0:1; // 0 Chua gui MT, 1 da gui MT // Nếu chưa gửi dc cần xây kế hoặc để gửi lại
           UpdateSMSLogByMe($rowSMSLog["id"],$Content,$Charging,$resultSendMT,$RequestId); 
       }
       
       $datalog .="|response=".$response."|resultSendMT=".$resultSendMT;
       writeLog($datalog,"/logSmsMTMe.txt");  
       return strval($response);
    }

    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ""; 
    $server->service($HTTP_RAW_POST_DATA);
    exit();
?>
